package com.yc.web.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @author zwei
 * @create 2024-06-16 14:30
 * 用于监听session的变化情况
 */
@WebListener
public class ResSessionChangeListener implements HttpSessionAttributeListener {
    public ResSessionChangeListener(){
        System.out.println("ResSessionChangeListener的构造方法,看我什么时候执行，执行了几次");
    }
    //session中属性的添加-》第一次 session.setAttribute("键",obj)
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("向session中添加了属性，内容为");
        String key=httpSessionBindingEvent.getName();
        Object obj=httpSessionBindingEvent.getValue();
        System.out.println(key+":"+obj);
    }
    //session中属性的删除  session.removeAttribute("键")
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("向session中删除了属性，内容为");
        String key=httpSessionBindingEvent.getName();
        Object obj=httpSessionBindingEvent.getValue();
        System.out.println(key+":"+obj);
    }
    //session中属性的替换 -》第二次 session.setAttribute("键",obj)
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("向session中替换了属性，内容为");
        String key=httpSessionBindingEvent.getName();
        Object obj=httpSessionBindingEvent.getValue();
        System.out.println(key+":"+obj);
    }
}
